package com.tsingteng.cosfun.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.helper.ViewHelper;
import com.tsingteng.cosfun.ui.focus.NoticeCountUtil;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.view.BadgeView;

/**
 * 描述:
 * 创建人: 肖利
 * 时间: 2018/7/30/6/28.
 * qq: (872179966)
 */

public class ButtomView extends FrameLayout implements View.OnClickListener {


    private LinearLayout mTab;
    private LinearLayout mHome;
    private LinearLayout mAtt;
    private LinearLayout mAdd;
    private LinearLayout mMessage;
    private LinearLayout mMine;
    private TextView mHomeLine;
    private TextView mAttLine;
    private TextView mMessageLine;
    private TextView mMineLine;
    private TextView mTvHome;
    private TextView mTvAtt;
    private TextView mTvMessage;
    private TextView mTvMine;
    private TextView mTvFoundNumber;
    private Context context;
    private OnSelectBottomListener onSelectBottomListener;
    private LinearLayout selectView;
    private TextView mTvRedNumber;
    private TextView mTvFollow;

    public ButtomView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public ButtomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ButtomView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_button, null);
        LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        mTab = view.findViewById(R.id.ll_tab_dy);
        mHome = view.findViewById(R.id.ll_home);
        mAtt = view.findViewById(R.id.ll_att);
        mAdd = view.findViewById(R.id.ll_add);
        mMessage = view.findViewById(R.id.ll_message);
        mMine = view.findViewById(R.id.ll_mine);
        mHomeLine = view.findViewById(R.id.tv_home_line);
        mAttLine = view.findViewById(R.id.tv_att_line);
        mMessageLine = view.findViewById(R.id.tv_message_line);
        mMineLine = view.findViewById(R.id.tv_mine_line);
        mTvHome = view.findViewById(R.id.tv_home);
        mTvAtt = view.findViewById(R.id.tv_att);
        mTvMessage = view.findViewById(R.id.tv_message);
        mTvMine = view.findViewById(R.id.tv_mine);
        mTvFoundNumber=view.findViewById(R.id.tv_found_number);
        mTvRedNumber = view.findViewById(R.id.tv_red_number);
        mTvFollow = view.findViewById(R.id.tv_follow_number);
        mHome.setOnClickListener(this);
        mAtt.setOnClickListener(this);
        mAdd.setOnClickListener(this);
        mMessage.setOnClickListener(this);
        mMine.setOnClickListener(this);
        addView(view);
        resetLine(false, mHomeLine);
        selectView = mHome;
        setAnimtor(mHome);
        mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
        if (LogonUtils.getIsLogin()) {
            mTvRedNumber.setVisibility(AppUtils.getMessageNumber() ? VISIBLE : GONE);
            if (AppUtils.getFollowCount()==0) {
                mTvFollow.setVisibility(GONE);
            }else {
                mTvFollow.setVisibility(VISIBLE);
                if (AppUtils.getFollowCount()>99) {
                    mTvFollow.setText(99+"");
                }else {
                    mTvFollow.setText(AppUtils.getFollowCount()+"");
                }

            }
        }else {
            mTvRedNumber.setVisibility(GONE);
            mTvFollow.setVisibility(GONE);
        }

        setTextColor(mTvHome);
//        NoticeCountUtil.setGuanzhuCount(context, mAtt, 10);//设置消息数量
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                if (onSelectBottomListener != null) {
                    setAnimtor(mHome);
                    resetLine(true, null);
                    onSelectBottomListener.onSelectHome();
                    resetLine(false, mHomeLine);
                    mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
                    setTextColor(mTvHome);
                }
                break;
            case R.id.ll_att:
//                if (!LogonUtils.getIsLogin()) {
//                    setAnimtor(mAtt);
//                    Navigate.startLogon(context,true);
//                    return ;
//                }
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectAtt()) {
                        setAnimtor(mAtt);
                        resetLine(true, null);
                        resetLine(false, mAttLine);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
                        setTextColor(mTvAtt);
                        AppUtils.setFollowCount(0);//取消显示数量
                        mTvFollow.setVisibility(GONE);
                    }
                }
                break;
            case R.id.ll_add:

                if (onSelectBottomListener != null) {
                    onSelectBottomListener.onSelectAdd();
                }
                break;
            case R.id.ll_message:
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectMessage()) {
                        setAnimtor(mMessage);
                        resetLine(true, null);
                        resetLine(false, mMessageLine);
                        setTextColor(mTvMessage);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.black));
                    }

                }
                break;
            case R.id.ll_mine:
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectMine()) {
                        setAnimtor(mMine);
                        resetLine(true, null);
                        resetLine(false, mMineLine);
                        setTextColor(mTvMine);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.black));
                    }
                }
                break;
        }
    }

    public void resetLine(boolean isReset, TextView line) {
        if (LogonUtils.getIsLogin()) {
            mTvRedNumber.setVisibility(AppUtils.getMessageNumber() ? VISIBLE : GONE);
            mTvFollow.setVisibility(AppUtils.getFollowCount()==0 ? GONE : VISIBLE);
        }else {
            mTvFollow.setVisibility(GONE);
            mTvRedNumber.setVisibility(GONE);
        }
        if (isReset) {
            mHomeLine.setVisibility(INVISIBLE);
            mAttLine.setVisibility(INVISIBLE);
            mMessageLine.setVisibility(INVISIBLE);
            mMineLine.setVisibility(INVISIBLE);
        } else {
            resetLine(!isReset, line);
            if (line == null) return;
            line.setVisibility(VISIBLE);
        }

    }


    public void setButtomUI(int type) {
        switch (type) {
            case 0:
                if (onSelectBottomListener != null) {
                    setAnimtor(mHome);
                    resetLine(true, null);
                    onSelectBottomListener.onSelectHome();
                    resetLine(false, mHomeLine);
                    mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
                    setTextColor(mTvHome);
                }
                break;
            case 1:
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectAtt()) {
                        setAnimtor(mAtt);
                        resetLine(true, null);
                        resetLine(false, mAttLine);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
                        setTextColor(mTvAtt);
                    }
                }
                break;
            case 2:
                if (onSelectBottomListener != null) {
                    onSelectBottomListener.onSelectAdd();
                }
                break;
            case 3:
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectMessage()) {
                        setAnimtor(mMessage);
                        resetLine(true, null);
                        resetLine(false, mMessageLine);
                        setTextColor(mTvMessage);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.black));
                    }

                }
                break;
            case 4:
                if (onSelectBottomListener != null) {
                    if (onSelectBottomListener.onSelectMine()) {
                        setAnimtor(mMine);
                        resetLine(true, null);
                        resetLine(false, mMineLine);
                        setTextColor(mTvMine);
                        mTab.setBackgroundColor(context.getResources().getColor(R.color.black));
                    }
                }
                break;
        }
    }

    public void setOnSelectBottomListener(OnSelectBottomListener onSelectBottomListener) {
        this.onSelectBottomListener = onSelectBottomListener;
    }

    public interface OnSelectBottomListener {
        void onSelectHome();

        boolean onSelectAtt();

        void onSelectAdd();

        boolean onSelectMessage();

        boolean onSelectMine();
    }

    public void setTabBgColor(int rId) {
        mTab.setBackgroundColor(context.getResources().getColor(rId));
    }


    private void setTextColor(TextView textColor) {
        resetTextColor();
        textColor.setSelected(true);
//        textColor.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    private void resetTextColor() {
        mTvHome.setSelected(false);
//        mTvHome.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mTvAtt.setSelected(false);
//        mTvAtt.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mTvMessage.setSelected(false);
//        mTvMessage.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        mTvMine.setSelected(false);
//        mTvMine.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

    }

    public void goHomeFocus() {
        setTextColor(mTvHome);
        resetLine(false, mHomeLine);
        mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
    }
    public void goMainFocus() {
        setTextColor(mTvHome);
        resetLine(false, mHomeLine);
        mTab.setBackgroundColor(context.getResources().getColor(R.color.trank));
    }

    /**
     * 放大动画
     *
     * @param view
     */
    public void setDYCaleBig(View view) {
//        ViewHelper.setScaleX(view, 1.05f);
//        ViewHelper.setScaleY(view, 1.05f);
    }

    /**
     * 缩小动画
     *
     * @param view
     */
    public void setDYCaleSmall(View view) {
//        ViewHelper.setScaleX(view, 1f);
//        ViewHelper.setScaleY(view, 1f);
    }

    private void setAnimtor(LinearLayout view) {
        if (selectView == view) return;
        resetAnimtor();
        setDYCaleBig(view);
        selectView = view;

    }

    private void resetAnimtor() {
        setDYCaleSmall(mHome);
        setDYCaleSmall(mAtt);
        setDYCaleSmall(mAdd);
        setDYCaleSmall(mMessage);
        setDYCaleSmall(mMine);
    }

    public void setReadCount(String count){
        if (null == mTvFoundNumber){
            return;
        }
        mTvFoundNumber.setText(count);
    }
    public void setVisible(){
        mTvFoundNumber.setVisibility(VISIBLE);
    }
    public void setGone(){
        mTvFoundNumber.setVisibility(GONE);
    }

}
